package com.caterina.springBoard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CLabel;
import com.caterina.components.CPanel;
import com.caterina.resources.CColors;
import com.caterina.resources.CFont;
import com.caterina.resources.CWords;

import controllers.PermissionList;

public class Category {
	public Category(String tag,String icon,Link...buttons){
		this.tag=tag;
		this.buttons=new ArrayList<Link>();
		for(Link btn:buttons){this.buttons.add(btn);}
		this.icon=ICON_PATH+icon+ICON_EXT;
	}
	public Collection<Link> getButtons() {
		return buttons;
	}
	public String getTag() {
		return tag;
	}
	public boolean canView(PermissionList permissions){
		for (Link btn: buttons){
			if (btn.canView(permissions))return true;
		}
		return false;
	}
	public CPanel getCategoryPanel(CActivity caller,PermissionList permissions){
		if(this.canView(permissions)) return this.renderPanel(caller,permissions);
		else return null;
	}
	private CPanel renderPanel(CActivity caller,PermissionList permissions){
		BorderLayout bl=new BorderLayout();
		bl.setVgap(20);bl.setHgap(3);
		final CPanel retPanel=new CPanel(bl);
		retPanel.setBackground(CColors.BUTTON_NORMAL_CATERINA);
		CPanel linksPanel=new CPanel();
		linksPanel.setOpaque(false);
		linksPanel.setLayout(new BoxLayout(linksPanel, BoxLayout.Y_AXIS));
		for(Link link:buttons){
			if (link.canView(permissions)){
				linksPanel.add(link.getButton(caller, permissions));
			}
		}
		CLabel title=new CLabel(" "+CWords.getWord(tag));
		title.setFont(CFont.CLABEL_H2);
		title.setForeground(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		final CPanel titlePanel =new CPanel();
		titlePanel.add(title,BorderLayout.CENTER);
		titlePanel.setMinimumSize(new Dimension(0,25));
		titlePanel.setOpaque(false);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, CColors.white));
		retPanel.add(linksPanel,BorderLayout.CENTER);
		retPanel.add(createIcon(),BorderLayout.WEST);
		retPanel.add(titlePanel,BorderLayout.NORTH);
		retPanel.setMaximumSize(new Dimension(1000,170));
		retPanel.setBorder(BorderFactory.createMatteBorder(2, 1, 1, 1, Color.GRAY));
		retPanel.addMouseListener(new MouseListener() {	
			@Override
			public void mouseReleased(MouseEvent arg0) {}
			@Override
			public void mousePressed(MouseEvent arg0) {}	
			@Override
			public void mouseExited(MouseEvent arg0) {};
			@Override
			public void mouseEntered(MouseEvent arg0) {}
			@Override
			public void mouseClicked(MouseEvent arg0) {}
		});
		return retPanel;
	}
	private CPanel createIcon(){
		CPanel iconPanel=new CPanel();
		iconPanel.setOpaque(false);
		ImageIcon icn=new ImageIcon(icon);
		icn=new ImageIcon(icn.getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH));
		JLabel lblIcn=new JLabel();
		lblIcn.setIcon(icn);
		iconPanel.add(lblIcn);
		return iconPanel;
	}	
	public void setParent(Container c){
		for(Link l:buttons){
			l.setParent(c);
		}
	}
	private static final String ICON_PATH="graphics/buttons/";
	private static final String ICON_EXT=".png";
	private String icon;
	private ArrayList<Link> buttons;
	private String tag;
}